<?php
namespace Duobao\Model;

use Com\Weixin\Qrcode;
use Think\Model;
use Duobao\Controller\LogController as Mylog;

class PartnerModel extends Model{
    protected $tableName = 'partner';
    /**
     * 获取用户的佣金
     */
    public function getCommission($uid){
        #定义返回数组
        $ret = array();
        $condition['uid'] = $uid;
        #1、兼容性的考虑，以前用户存在进入系统后没有合火人模块
        $result = $this->where($condition)->find();
        if($result){
           $ret['t_com'] = $result['t_commission'];#总佣金
           $ret['s_com'] = $result['s_commission'];#剩余佣金
           $ret['a_com'] = $result['a_commission'];#已提现的佣金
        }else{
            #添加用户的合活人佣金信息
            $data['uid'] = $uid;
            $data['t_commission'] = 0;
            $data['s_commission'] = 0;
            $data['a_commission'] = 0;

            $addStatus = $this->add($data);#返回插入的主键值
            if(!$addStatus){
                E('发生了一个错误',403);
            }else{
                $ret['t_com'] = '0.00';#总佣金
                $ret['s_com'] = '0.00';#剩余佣金
                $ret['a_com'] = '0.00';#已提现的佣金
            }
        }

        return $ret;
    }


    /**
     * 考虑到使用member的分别获取用户名和图像的数据库操作是两次操作，故在才直接定义了一个方法返回用户的姓名和图像
     */
    public function getUserMsg($uid){
        #定义返回数组
        $ret = array();
        $model = new \Think\Model();
        $condition['uid'] = $uid;
        $result = $model->table('__MEMBER__')->where($condition)->field('username,img')->find();
        #echo $model->getLastSql();
        if($result){
            #判断用户名是否存在，如果不存在显示“未填写用户昵称”
            if($result['username']){
                $ret['username'] = $result['username'];
            }else{
                $ret['username'] = '未填写用户昵称';
            }
            #判断用户图像是否存在
            if($result['img']){
                $ret['img'] = $result['img'];
            }else{
                $ret['img'] = C('CDN_DOMAIN').C("UPLOAD_PATH").$result['img'];
            }
        }else{
            E('发生了一个错误',403);
        }

        return $ret;
    }


    /**
     * 生成专属二维码
     * 思路：前后端跨域的问题，session起不了任何作用
     * 临时生成二维码并读取二维码的数据进行编码后存储到数据库中
     */
    public function productQrcode($uid){
        $ret = '';
        $condition['uid'] = $uid;
        $result = $this->where($condition)->find();
        if($result){
            #判断用户二维码是否存在
            if(!$result['qrcode']){
                $qrcode = new Qrcode();

                #判断tmp下的缓存目录是否存在,暂存
                if(!is_dir(C('qrcode'))){
                    mkdir(C('qrcode'));
                }

                $imgName = C('qrcode').time().rand(1000,9999).'.png';
                $qrcode->png('140', $imgName, $level = QR_ECLEVEL_L, $size = 10, $margin = 10, $saveandprint=true);

                #读取图片的数据进行base64编码后存入数据库中
                $fp = file_get_contents($imgName);
                $base64 = base64_encode($fp);
                #插入二维码
                $where['uid'] = $uid;
                $data['qrcode'] = $base64;

                $this->where($where)->save($data);
                unlink($imgName);
                $ret = $data['qrcode'];
            }else{
                #直接返回给用户二维码数据
                $ret = $result['qrcode'];
            }
        }else{
            #用户不存在的情况下，先插入用户数据
            $qrcode = new Qrcode();

            #判断tmp下的缓存目录是否存在,暂存
            if(!is_dir(C('qrcode'))){
                mkdir(C('qrcode'));
            }

            $imgName = C('qrcode').time().rand(1000,9999).'.png';
            $qrcode->png('140', $imgName, $level = QR_ECLEVEL_L, $size = 10, $margin = 10, $saveandprint=true);

            #读取图片的数据进行base64编码后存入数据库中
            $fp = file_get_contents($imgName);
            $base64 = base64_encode($fp);

            $data['uid'] = $uid;
            $data['t_commission'] = 0;
            $data['a_commission'] = 0;
            $data['s_commission'] = 0;
            $data['qrcode'] = $base64;
            #Mylog::write($base64);
            $this->add($data);
            #Mylog::write($this->getLastSql());
            unlink($imgName);
            $ret = $data['qrcode'];
        }

        #销毁图片
        return $ret;

    }


    /**
     * 生成专属二维码
     * 二维码直接存储在硬盘上
     */
    public function productQrcode2($uid){
        $ret = '';
        $condition['uid'] = $uid;
        $result = $this->where($condition)->find();
        if($result){
            #判断用户二维码是否存在
            if(!$result['qrcode']){
                $qrcode = new Qrcode();

                #判断tmp下的缓存目录是否存在,暂存
                if(!is_dir(C('qrcode'))){
                    mkdir(C('qrcode'));
                }

                $imgName = C('qrcode').'/'.time().rand(1000,9999).'.png';
                #二维码连接地址
                $urladdr = C('APP_URL').'/Partner/showBind/?uid='.$uid;
                $qrcode->png($urladdr, $imgName, QR_ECLEVEL_L, 10, 10, true);

                #插入二维码
                $where['uid'] = $uid;
                $data['qrcode'] = $imgName;

                $this->where($where)->save($data);
                $ret = $data['qrcode'];
            }else{
                #直接返回给用户二维码数据
                $ret = $result['qrcode'];
            }
        }else{
            #用户不存在的情况下，先插入用户数据
            $qrcode = new Qrcode();

            #判断tmp下的缓存目录是否存在,暂存
            if(!is_dir(C('qrcode'))){
                mkdir(C('qrcode'));
            }

            $imgName = C('qrcode').'/'.time().rand(1000,9999).'.png';
            $urladdr = C('APP_URL').'/Partner/showBind/?uid='.$uid;
            $qrcode->png($urladdr, $imgName, QR_ECLEVEL_L, 10, 10, true);


            $data['uid'] = $uid;
            $data['t_commission'] = 0;
            $data['a_commission'] = 0;
            $data['s_commission'] = 0;
            $data['qrcode'] = $imgName;
            #Mylog::write($base64);
            $this->add($data);
            #Mylog::write($this->getLastSql());
            $ret = $data['qrcode'];
        }
        return $ret;
    }

    /**
     * 获取各个等级的合伙人的人数和为自己产生的财富
     */
    public function getPartnerData($uid){
        #定义返回数组
        $ret = array();
        $model = new \Think\Model();

        #获取核心合伙人的人数和产生的财富
        $ret['corecount'] = $model->query("select count(id) as countcore from __PREFIX__partner_relationship WHERE bind_status = 1 and pid =".$uid)[0]['countcore'];
       # Mylog::write($model->getLastSql());
        #获取高级合伙人的人数和产生的财富
        $ret['heightcount'] = $model->query("select count(id) as countheight from __PREFIX__partner_relationship WHERE bind_status = 1 and gpid =".$uid)[0]['countheight'];
        #获取普通合伙人的人数和产生的财富
        $ret['lowcount'] = $model->query("select count(id) as countlow from __PREFIX__partner_relationship WHERE bind_status = 1 and ggpid =".$uid)[0]['countlow'];

        $where['uid'] = $uid;
        $result = $model->query("select `h_partner`,`m_partner`,`s_partner` from __PREFIX__partner WHERE uid = $uid");

        $ret['coremoney'] = $result[0]['h_partner'];
        $ret['heightmoney'] = $result[0]['m_partner'];
        $ret['lowmoney'] = $result[0]['s_partner'];

        return $ret;
    }
}
